package com.lunduhotel.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.lunduhotel.exceptions.ExceptionForeignKey;
import com.lunduhotel.factory.ConnectionFactory;
import com.lunduhotel.modelo.Quarto;

public class QuartoDao {
   private Connection conexao;
   
   public QuartoDao(){
	    ConnectionFactory factory  = new ConnectionFactory();
	    conexao = factory.getConnection();
   }
   public void adicionarQuarto(Quarto quarto){
	   try{
		  String sql = "insert into quarto_hotel(id_quarto,tipo_quarto,valor_diaria,quant_cama,quant_cama_casal) values (?,?,?,?,?);";
		  PreparedStatement stmt;
		  stmt= conexao.prepareStatement(sql);
		  
		  stmt.setInt(1, quarto.getId());
		  stmt.setString(2, quarto.getTipo());
		  stmt.setFloat(3, quarto.getValor());
		  stmt.setInt(4, quarto.getQuantCama());
		  stmt.setInt(5,quarto.getQuantCamaCasal());
		  
		  stmt.execute();
		   
		   
		   
	   }catch(SQLException e){
		   e.printStackTrace();
		   throw  new RuntimeException();
	   
	   }
	   
	   
   }
   public void RemoverQuarto(int id){
	   try {
		 String sql = "delete from quarto_hotel where id_quarto=?;";
		 PreparedStatement stmt;
		 stmt= conexao.prepareStatement(sql);
		 
		 stmt.setInt(1, id);
		 
		 
		 stmt.execute();
	} catch (SQLException e) {
		if(e.getMessage().contains("violates foreign key")){
			throw new ExceptionForeignKey("Existem reservas para esse quarto");
		}else{
			throw new RuntimeException();
		}
	
	} 
	   
	   
   }
   
   public void AtualizarQuarto(Quarto quarto){
	   try {
		 String sql = "update quarto set tipo_quarto=?,valor_diaria=?,quant_cama=?,quant_cama_casal=?;";
		 PreparedStatement stmt;
		 stmt= conexao.prepareStatement(sql);
		 
		 stmt.setString(2, quarto.getTipo());
		 stmt.setFloat(3, quarto.getValor());
		 stmt.setInt(4,quarto.getQuantCama());
		 stmt.setInt(5, quarto.getQuantCamaCasal());
		 
		 stmt.execute();
		 
	  } catch (Exception e) {
		e.printStackTrace();
		throw new RuntimeException();
	   
	   
       }
   }
  
   public List<Quarto> getLista(){
	   List<Quarto> list = new ArrayList<Quarto>();
	   
	   String sql = "select id_quarto,tipo_quarto,valor_diaria,quant_cama,quant_cama_casal from quarto_hotel;";
	   
	   try {
		ResultSet rs = conexao.prepareStatement(sql).executeQuery();
		while(rs.next()){
				Quarto quarto = new Quarto();
				quarto.setId(rs.getInt(1));
				quarto.setTipo(rs.getString(2));
				quarto.setValor(rs.getFloat(3));
				quarto.setQuantCama(rs.getInt(4));
				quarto.setQuantCamaCasal(rs.getInt(5));
				
				list.add(quarto);
				
		}
		
	} catch (SQLException e) {
		e.printStackTrace();
		throw new RuntimeException();
	}
	   
	   return list;
   }
   
   public List<Quarto> getListaTipo(String tipo){
	   List<Quarto> list = new ArrayList<Quarto>();
	   
	   String sql = "select id_quarto,tipo_quarto,valor_diaria,quant_cama,quant_cama_casal from quarto_hotel " +
	   		"where tipo_quarto = ?;";
	   
	   try {
		   
		PreparedStatement stmt = conexao.prepareStatement(sql);
		stmt.setString(1, tipo);
		ResultSet rs = stmt.executeQuery();
		while(rs.next()){
				Quarto quarto = new Quarto();
				quarto.setId(rs.getInt(1));
				quarto.setTipo(rs.getString(2));
				quarto.setValor(rs.getFloat(3));
				quarto.setQuantCama(rs.getInt(4));
				quarto.setQuantCamaCasal(rs.getInt(5));
				
				list.add(quarto);
				
		}
		
	} catch (SQLException e) {
		e.printStackTrace();
		throw new RuntimeException();
	}
	   
	   return list;
   }
   
   
}   
   

